package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.annotation.CacheLevel;
import com.okhttplib.callback.CallbackOk;
import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.CityAdapter;
import com.wqtz.chechai.api.Constans;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.City;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.JsonUtils;
import com.wqtz.chechai.view.impl.DownloadSuccessImpl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/23 15:50
 */

public class NewAddStepTwoCarState2 extends BaseActivity implements CityAdapter.OnItemLickListener {
	String proID;
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.tv_title)
	TextView mTvTitle;
	@BindView(R.id.tv_province)
	TextView mTvProvince;
	@BindView(R.id.rv_city)
	RecyclerView mRvCity;
	private City c;
	CityAdapter madapter;
	List<City.ResultBean> mcitylist;
	List<com.wqtz.chechai.dao.City> mdaoCitylist;

	@Override
	public void init() {
		ButterKnife.bind(this);
		Intent intent = getIntent();
		proID = intent.getStringExtra("proID");
		mTvProvince.setText(intent.getStringExtra("provinceName"));
		getCityInfo(proID, new DownloadSuccessImpl() {
			@Override
			public void success(City city) {
				c = city;
				Logger.e(c.toString()+c.getResult().size());
				mcitylist = new ArrayList<>();
				for (int i = 0; i <c.getResult().size() ; i++) {
					mcitylist.add(c.getResult().get(i));
				}
				madapter = new CityAdapter(R.layout.item_province,mcitylist);
				madapter.setOnItemClickListen(NewAddStepTwoCarState2.this);
				mRvCity.setLayoutManager(new GridLayoutManager(NewAddStepTwoCarState2.this, 1));
				mRvCity.addItemDecoration(new MyItemDecoration());
				mRvCity.setAdapter(madapter);
			}
		});




	}

	@Override
	public void loadData() {


	}

	@Override
	public int setContentViews() {
		return R.layout.fragment_newaddsteptwocar2;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}

	public City getCityInfo(final String proID, final DownloadSuccessImpl d) {
		String url = String.format(Constans.CITY, "", proID);
		final City[] cityinfo = {new City()};
		//加载数字
		OkHttpUtil.Builder().setCacheLevel(CacheLevel.FIRST_LEVEL).setConnectTimeout(500).build().doGetAsync(
				HttpInfo.Builder().setUrl(url).build(), new CallbackOk() {
					@Override
					public void onResponse(HttpInfo info) throws IOException {
						if (info.isSuccessful()) {
							String result = info.getRetDetail();
							if (result != null) {
								//SDCardUtils.saveFileToExternalCacheDir(WQTZApplication.getApplication(),result.getBytes(),""+proID+".json");
								cityinfo[0] = JsonUtils.getCityInfoBean(result);
								d.success(cityinfo[0]);
							} else {
								cityinfo[0] = null;
							}

						} else {
							cityinfo[0] = null;
						}

					}
				});
		return cityinfo[0];
	}




	@OnClick(R.id.iv_back)
	public void onClick() {
		finish();
	}

	@Override
	public void onItemLick(int position) {
		Intent intent = new Intent();
		intent.putExtra("city_name",mcitylist.get(position).getCityName());
		intent.putExtra("city_id",mcitylist.get(position).getCityID());
		intent.putExtra("pro_id",mcitylist.get(position).getProID());
		setResult(RESULT_OK,intent);
		finish();
	}

	class MyItemDecoration extends RecyclerView.ItemDecoration {
		/**
		 *
		 * @param outRect 边界
		 * @param view recyclerView ItemView
		 * @param parent recyclerView
		 * @param state recycler 内部数据管理
		 */
		@Override
		public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
			//设定底部边距为1px
			outRect.set(0, 0, 0, 1);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
	}
}
